import 'package:flutter_riverpod/flutter_riverpod.dart';

enum KeyboardType {
  province, //省份
  letter, //字母
  numberLetter, //字母和数字
}

class NumberCarModel {
  KeyboardType keyboardType;

  ///第几个框有值
  int index;

  ///第几个框正在输入
  int inputIndex;

  ///省份
  String value0;

  ///字母
  String value1;

  ///字母和数字
  String value2;
  String value3;
  String value4;
  String value5;
  String value6;

  ///新能源
  String value7;

  NumberCarModel({
    this.keyboardType = KeyboardType.province,
    this.index = -1,
    this.inputIndex = 0,
    this.value0 = "",
    this.value1 = "",
    this.value2 = "",
    this.value3 = "",
    this.value4 = "",
    this.value5 = "",
    this.value6 = "",
    this.value7 = "",
  });

  // Since NumberCarModel is immutable, we implement a method that allows cloning the
  // NumberCarModel with slightly different content.
  NumberCarModel copyWith({
    KeyboardType? keyboardType,
    int? index,
    int? inputIndex,
    String? value0,
    String? value1,
    String? value2,
    String? value3,
    String? value4,
    String? value5,
    String? value6,
    String? value7,
  }) {
    return NumberCarModel(
      keyboardType: keyboardType ?? this.keyboardType,
      index: index ?? this.index,
      inputIndex: inputIndex ?? this.inputIndex,
      value0: value0 ?? this.value0,
      value1: value1 ?? this.value1,
      value2: value2 ?? this.value2,
      value3: value3 ?? this.value3,
      value4: value4 ?? this.value4,
      value5: value5 ?? this.value5,
      value6: value6 ?? this.value6,
      value7: value7 ?? this.value7,
    );
  }
}

class NumberCarProvider extends StateNotifier<NumberCarModel> {
  NumberCarProvider() : super(NumberCarModel());

  ///返回车牌号
  String getNumberCar() {
    return (state.value0 +
        state.value1 +
        state.value2 +
        state.value3 +
        state.value4 +
        state.value5 +
        state.value6 +
        state.value7);
  }

  ///返回车牌号带 · 的
  String getPointNumberCar() {
    return (state.value0 +
        state.value1 +
        "·" +
        state.value2 +
        state.value3 +
        state.value4 +
        state.value5 +
        state.value6 +
        state.value7);
  }

  ///是否已经输入完了车牌号
  bool isNumberCar() {
    return state.value0.isNotEmpty &&
        state.value1.isNotEmpty &&
        state.value2.isNotEmpty &&
        state.value3.isNotEmpty &&
        state.value4.isNotEmpty &&
        state.value5.isNotEmpty &&
        state.value6.isNotEmpty;
  }

  ///添加操作
  addVaule(String value) {
    if (state.value7.isNotEmpty) return;
    switch (state.inputIndex) {
      case 0:
        state.value0 = value;
        break;
      case 1:
        state.value1 = value;
        break;
      case 2:
        state.value2 = value;
        break;
      case 3:
        state.value3 = value;
        break;
      case 4:
        state.value4 = value;
        break;
      case 5:
        state.value5 = value;
        break;
      case 6:
        state.value6 = value;
        break;
      case 7:
        state.value7 = value;
        break;
    }
    if (state.index < 8) {
      state.index++;
      state.inputIndex++;
    }
    if (state.inputIndex == 0) {
      state.keyboardType = KeyboardType.province;
    } else if (state.inputIndex == 1) {
      state.keyboardType = KeyboardType.letter;
    } else {
      state.keyboardType = KeyboardType.numberLetter;
    }

    state = state.copyWith();
  }

  ///删除操作
  delete() {
    if (state.value0.isEmpty) return;
    switch (state.index) {
      case 0:
        state.value0 = "";
        break;
      case 1:
        state.value1 = "";
        break;
      case 2:
        state.value2 = "";
        break;
      case 3:
        state.value3 = "";
        break;
      case 4:
        state.value4 = "";
        break;
      case 5:
        state.value5 = "";
        break;
      case 6:
        state.value6 = "";
        break;
      case 7:
        state.value7 = "";
        break;
    }

    if (state.inputIndex > 0) {
      state.index--;
      state.inputIndex--;
    }
    if (state.inputIndex == 0) {
      state.keyboardType = KeyboardType.province;
    } else if (state.inputIndex == 1) {
      state.keyboardType = KeyboardType.letter;
    } else {
      state.keyboardType = KeyboardType.numberLetter;
    }
    state = state.copyWith();
  }

  ///更新号码 并且不能交互
  assignNumber(String carNumber) {
    var list = carNumber.split("");
    for (int i = 0; i < list.length; i++) {
      switch (i) {
        case 0:
          state.value0 = list[i];
          break;
        case 1:
          state.value1 = list[i];
          break;
        case 2:
          state.value2 = list[i];
          break;
        case 3:
          state.value3 = list[i];
          break;
        case 4:
          state.value4 = list[i];
          break;
        case 5:
          state.value5 = list[i];
          break;
        case 6:
          state.value6 = list[i];
          break;
        case 7:
          state.value7 = list[i];
          break;
      }
    }
    state.index = list.length;
    state.inputIndex = list.length + 1;
    state = state.copyWith();
  }
}

final numberCarProvider =
    StateNotifierProvider.autoDispose<NumberCarProvider, NumberCarModel>((ref) {
  return NumberCarProvider();
});
